/*
 * Copyright (c) zhg2yqq.com Corp.
 * All Rights Reserved.
 */
package com.zhg2yqq.wheels.security;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.lang.Nullable;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;

import com.zhg2yqq.wheels.common.dto.CurrentUserDTO;

/**
 * 可接收处理认证事件
 *
 * @author zhg2yqq, 2022年12月2日
 * @version zhg2yqq v1.0
 */
public interface ISecurityEventHook {
    default void anonymousEvent(HttpServletRequest request) {
    }

    default void loginFailureEvent(HttpServletRequest request, AuthenticationException e) {
    }

    default void loginSuccessEvent(CurrentUserDTO principal, HttpServletRequest request) {
    }

    default void accessDeniedEvent(CurrentUserDTO principal, HttpServletRequest request) {
    }

    default void logoutSuccessEvent(CurrentUserDTO principal, HttpServletRequest request) {
    }

    default void loginInvalidEvent(CurrentUserDTO principal, HttpServletRequest request) {
    }

    /**
     * 删除储存的认证信息回调
     *
     * @param token          删除的认证唯一标识
     * @param authentication 删除认证信息，未获取到认证信息时为null
     */
    default void catchClearLoginUser(String token, @Nullable Authentication authentication) {
    }

    /**
     * 存储认证信息时回调
     *
     * @param token          认证唯一标识
     * @param authentication 认证信息
     * @param ttl            单位秒
     */
    default void catchSaveLoginUser(String token, Authentication authentication, int ttl,
                                    HttpServletRequest request, HttpServletResponse response) {
    }

    default void catchAuthenticationException(AuthenticationException ex,
                                              HttpServletRequest request,
                                              HttpServletResponse response) {
    }
}
